﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;
using UDS.Entity;

namespace UDS.SubModule.Position
{
    /// <summary>
    /// NewStaff 的摘要说明。
    /// </summary>
    public partial class NewStaffPage : Page
    {
        private int EditStatus;
        protected string PositionID;
        public int ReturnPage;
        private int sex = 1;
        private int StaffID;
        protected string Username;

        protected override void OnInit(EventArgs e)
        {
            Username = SiteContext.Current.UserName;
            if (Request.QueryString["ReturnPage"] != null)
            {
                ReturnPage = Int32.Parse(Request.QueryString["ReturnPage"]);
            }
            else
            {
                ReturnPage = 0;
            }

            //绑定部门
            BindPosition();

            //修改于2003-10-8日 目的：改正生日103岁问题
            //txtBirthday.Text = DateTime.Now.ToShortDateString();
            if (Request.QueryString["PositionID"] != null)
            {
                PositionID = Request.QueryString["PositionID"];
                SelectPosition(Int32.Parse(PositionID));
            }
            else
            {
                PositionID = "0";
            }

            if (Request.QueryString["StaffID"] != null)
            {
                StaffID = Int32.Parse(Request.QueryString["StaffID"]);
                GetStaffInfo(StaffID);
                EditStatus = 1;
                cbRemind.Visible = false;
                //cboPosition.Visible =false;
                cboPosition.Enabled = false;
            }
            else
            {
                EditStatus = 0;
                sex = 1;
            }

            // Add event handlers
            cmdSubmit.Click += cmdSubmit_Click;
            base.OnInit(e);
        }

        private void BindPosition()
        {
            var db = new Database();
            using (SqlDataReader dr_position = db.RunDataReader("dbo.sproc_GetAllPosition"))
            {
                cboPosition.DataSource = dr_position;
                cboPosition.DataTextField = "Position_Name";
                cboPosition.DataValueField = "Position_ID";
                cboPosition.DataBind();
            }
        }

        private void GetStaffInfo(int id)
        {
            txtPassword.TextMode = TextBoxMode.SingleLine;
            txtRePassword.TextMode = TextBoxMode.SingleLine;

            var staffInfo = Staff.GetStaffInfo(id);
            if (staffInfo != null)
            {
                txtStaffName.Text = staffInfo.Name;
                txtStaffName.ReadOnly = true;

                txtRealName.Text = staffInfo.RealName;
                if (staffInfo.Sex != Sex.Female)
                {
                    rb_male.Checked = true;
                    sex = 1;
                }
                else
                {
                    rb_female.Checked = true;
                    sex = 0;
                }

                txtBirthday.SelectedDate = staffInfo.Birthday;
                txtPassword.Text = staffInfo.Password;
                txtRePassword.Text = staffInfo.Password;
                txtEmail.Text = staffInfo.Email;
                txtPhone.Text = staffInfo.Phone;
                txtMobile.Text = staffInfo.Mobile;
                txtCaste.Text = staffInfo.Caste.ToString();
                PositionID = staffInfo.PositionID.ToString();

                SelectPosition(staffInfo.PositionID);
            }
        }

        private void SelectPosition(int id)
        {
            for (int i = 0; i < cboPosition.Items.Count; i++)
            {
                if (cboPosition.Items[i].Value == id.ToString())
                {
                    cboPosition.SelectedIndex = i;
                }
            }
        }

        private void cmdSubmit_Click(object sender, EventArgs e)
        {
            if (rb_male.Checked)
            {
                sex = 1;
            }
            else
            {
                sex = 0;
            }

            if (EditStatus == 0)
            {
                var db = new Database();
                if (myposition.Visible)
                {
                    PositionID = cboPosition.Items[cboPosition.SelectedIndex].Value;
                }

                var prams = new[]
                {
                    Database.MakeInParam("@StaffName", SqlDbType.NVarChar, 50, txtStaffName.Text),
                    Database.MakeInParam("@Password", SqlDbType.NVarChar, 255, txtPassword.Text),
                    Database.MakeInParam("@RealName", SqlDbType.NVarChar, 50, txtRealName.Text),
                    Database.MakeInParam("@Sex", SqlDbType.Int, 4, sex),
                    Database.MakeInParam("@Status", SqlDbType.Int, 4, 0),
                    Database.MakeInParam("@Email", SqlDbType.NVarChar, 300, txtEmail.Text),
                    Database.MakeInParam("@RegistedDate", SqlDbType.DateTime, 8, DateTime.Now),
                    Database.MakeInParam("@PositionID", SqlDbType.Int, 4, Int32.Parse(PositionID)),
                    Database.MakeInParam("@Phone", SqlDbType.NVarChar, 50, txtPhone.Text),
                    Database.MakeInParam("@Mobile", SqlDbType.NVarChar, 50, txtMobile.Text),
                    Database.MakeInParam("@Birthday", SqlDbType.DateTime, 8, Utility.GetValidSQLDate(txtBirthday.SelectedDate)),
                    Database.MakeInParam("@Caste", SqlDbType.Int, 4, Int32.Parse(txtCaste.Text))
                };
                using (SqlDataReader dr = db.RunDataReader("dbo.sproc_AddStaff", prams))
                {
                    if (dr.Read())
                    {
                        var sta = new Components.Staff();
                        if (cbRemind.Checked)
                        {
                            var staffList = Staff.GetAllStaff(false);
                            foreach (var staff in staffList)
                            {
                                var msg = cboPosition.SelectedItem.Text + "处新到员工 " + txtRealName.Text + "，特此通知。";
                                SMS.SendLocalMsg(Username, new string[] { staff.Name }, msg);
                            }
                        }
                        if (ReturnPage == 0)
                        {
                            Response.Redirect("ListView.aspx?Position_ID=" + PositionID);
                        }
                        else
                        {
                            Response.Redirect("../Staff/ManageStaff.aspx");
                        }
                    }
                    else
                    {
                        message.Text = "<font color='red'>该用户已经存在！</font>";
                    }
                }
            }
            else
            {
                var person = new Components.Staff();
                switch (person.UpdateInfo(StaffID, txtRealName.Text, sex, txtBirthday.Text == "-" ? "1900-01-01" : txtBirthday.Text, txtPassword.Text, txtEmail.Text, txtPhone.Text, txtMobile.Text, Int32.Parse(cboPosition.Items[cboPosition.SelectedIndex].Value), Int32.Parse(txtCaste.Text)))
                {
                    case 0:
                        PositionID = Int32.Parse(cboPosition.Items[cboPosition.SelectedIndex].Value).ToString();
                        if (ReturnPage == 0)
                        {
                            Response.Redirect("ListView.aspx?Position_ID=" + PositionID);
                        }
                        else
                        {
                            Response.Redirect("../Staff/ManageStaff.aspx");
                        }
                        message.Text = "修改成功！";
                        break;
                    case -1:
                        message.Text = "验证密码不对！";
                        break;
                }
            }
        }
    }
}